const { Paragraph, TextRun, CheckBox, TabStopType, Tab } = require("docx");
const { getAlignment, getIndentValue, generateUnderlineText } = require("../common/utils");

function generateTextareaField(field, baseIndent = 0) {
    const { value } = field;
    const finalIndent = getIndentValue(value.alignment, baseIndent);
    // 允许行数为0：当为0时不渲染下划线，仅渲染标题
    const rows = (value.rows ?? 1);
    
    const paragraphs = [];
    
    // 第一行包含字段名称（允许单独对齐）
    const nameAlignment = value.labelAlign ? getAlignment(value.labelAlign) : getAlignment(value.alignment);
    paragraphs.push(new Paragraph({
            indent: { left: finalIndent },
            alignment: nameAlignment,
        spacing: { before: 100, after: 100, line: 360 },
            children: [
                new TextRun({
                text: value.fieldName,
                size: 21,
                bold: value.isBold || false,
                font: "SimSun"
                })
            ]
    }));

    // 根据行数添加下划线
    for (let i = 0; i < rows; i++) {
        paragraphs.push(new Paragraph({
            indent: { left: finalIndent },
            alignment: getAlignment(value.alignment),
            spacing: { before: 50, after: 50, line: 360 },
            tabStops: [
                {
                    type: TabStopType.RIGHT,
                    position: 9000, // A reasonable value for A4 page width with margins
                },
            ],
            children: [
                new TextRun({
                    children: [new Tab()],
                    underline: {
                        type: "single",
                        color: "000000"
                    }
                })
            ]
        }));
    }
    
    return paragraphs;
}

module.exports = {
    generateTextareaField
}; 